package cn.zlp.basic.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagge接口配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {
		// 式swagger接口可以传入token登录信息,这样访问式就不会拦截了
		List<Parameter> pars = new ArrayList<>();
		pars.add(new ParameterBuilder()
				.name("token")
				.description("token")
				.required(false)
				.parameterType("header")
				.modelRef(new ModelRef("string"))
				.build()
		);
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				//对外暴露服务的包,以controller的方式暴露,所以就是controller的包.
				.apis(RequestHandlerSelectors.basePackage("cn.zlp"))
				.paths(PathSelectors.any())
				.build().globalOperationParameters(pars); // 最后一句最重要,代表全局加载参数
	}

	private ApiInfo apiInfo() {
		/*
			title:设置接口网页标题
			description:设置文章标题
			contact:联系信息
			version:版本
		 */
		return new ApiInfoBuilder()
			.title("宠物之家api")
			.description("宠物之家服务接口文档说明")
			.contact(new Contact("test", "", "2058508547@qq.com"))
			.version("1.0")
			.build();
	}
}